package controller

import (
	"heartbeat/database/model"
	"log"

	iris "gopkg.in/kataras/iris.v6"
)

type LoginUser struct {
	Name     string `form:"name" json:"name"`
	Password string `form:"password" json:"password"`
}

func LoginPost(ctx *iris.Context) {
	user := LoginUser{}
	if err := ctx.ReadForm(&user); err != nil {
		log.Println(err)
		ctx.SetStatusCode(500)
		return
	}
	userBean := new(model.UserBean)
	count, err := userBean.FindByUsernameAndPassword(user.Name, user.Password)
	if err != nil {
		ctx.SetStatusCode(500)
		log.Fatalln(err)
	}
	if count != 1 {
		ctx.SetStatusCode(403)
	}
	session := ctx.Session()
	session.Set("logined", true)
	session.Set("user", user)
	ctx.SetStatusCode(200)
}
